<template>
	<view class="container">
		<view class="navbar">
			<button type="default" plain class="talk-btn">
				<image
					src="https://mp-cc85421a-533b-4b68-b797-f0a3eba335d0.cdn.bspapp.com/order/order_icon_talk2.0.png">
				</image>
				<view>快来加入我们吧~~~</view>
			</button>
		</view>
		<view class="tabbar">
			<view class="item" :class="{active: !tabIndex}" @tap="switchTab(0)">当前订单</view>
			<view class="item" :class="{active: tabIndex}" @tap="switchTab(1)">历史订单</view>
		</view>
		<swiper :current="tabIndex" :duration="300" class="swiper" :show-scrollbar="false">
			<!-- 当前订单 begin -->
			<swiper-item @touchmove.stop="handleSwiperItemChange">
				<view class="no-order-content" v-if="currentOrders.length === 0">
					<image src="https://mp-cc85421a-533b-4b68-b797-f0a3eba335d0.cdn.bspapp.com/gif/happy5.gif"></image>
					<view class="tips">
						<view>您今天还没有下单</view>
						<view>快去选择一杯喜欢的茶吧</view>
					</view>
					<button type="primary" class="font-size-lg" @tap.stop="gotoxiadan"
						style="border-radius: 20rpx;">去下单</button>
				</view>
				<view class="current-order-content" v-else>
					<scroll-view scroll-y="true" class="current-orders-scroll">
						<view class="wrapper">
							<view class="order-list">
								<view class="order" v-for="(item, index) in currentOrders" :key="index">
									<view class="header">
										<view class="flex-fill font-size-medium">{{ item.address }}</view>
										<view class="status">
											<view>{{item.status}}</view>
											<image
												src="https://mp-cc85421a-533b-4b68-b797-f0a3eba335d0.cdn.bspapp.com/common/black_arrow_right.png">
											</image>
										</view>
									</view>
									<scroll-view scroll-x>
										<view class="images">
											<image :src="v" v-for="(v,i) in item.productImages" :key="i"></image>
										</view>
									</scroll-view>
									<view class="info">
										<view class="left">
											<view>订单编号：{{ item.no }}</view>
											<view>下单时间：{{ item.created_at }}</view>
										</view>
										<view class="right">
											￥{{ item.cartAmount }}
										</view>
									</view>
									<view class="action">
										<button style="border-radius: 50%;background-color: #ffbb0f;color: #ffffff;"
											plain hover-class="none" @click="qucan(index)">取餐码</button>
										<button type="default" hover-class="none"
											@tap="completeOrder(item)">商家完成订单</button>
										<button type="primary" plain hover-class="none">请稍等,马上好~</button>
										<uniPopup ref="popup" type="center">
											<view
												style="display: flex;justify-content: center;align-items: center;flex-direction: column;width: 500rpx;height: 500rpx;background-color: #ffffff;border-radius: 20rpx;">
												<view style="">
													<text
														style="font-weight: 900;font-size: 60rpx;transform: scaleX(2),scaleY(4);letter-spacing: 3rpx;">{{item.order_no}}</text>
												</view>
												<view class="qrcode">
													<view v-for="(row, rowI) in modules" :key="rowI"
														style="display: flex;flex-direction: row;">
														<view v-for="(col, colI) in row" :key="colI">
															<view v-if="col.isBlack"
																style="width: 6px;height: 6px;background-color: black;">
																<!-- 黑色码点 -->
															</view>
															<view v-else
																style="width: 6px;height: 6px;background-color: white;">
																<!-- 白色码点 -->
															</view>
														</view>
													</view>
												</view>
											</view>
										</uniPopup>
									</view>
								</view>
							</view>
						</view>
					</scroll-view>
				</view>
			</swiper-item>
			<!-- 当前订单 end -->
			<!-- 历史订单 begin -->
			<swiper-item @touchmove.stop="handleSwiperItemChange">
				<view class="history-order">
					<view class="menu">
						<view class="flex-fill d-flex justify-content-start">
							<view class="item" :class="{active: !orderMenuIndex}">门店订单</view>
							<!-- 							<view class="item" :class="{active: orderMenuIndex}" @tap="switchMenuTab(1)">百货订单</view> -->
						</view>
						<view class="item" v-show="batchInvoiceVisible">
							<image
								src="https://mp-cc85421a-533b-4b68-b797-f0a3eba335d0.cdn.bspapp.com/order/batch_invoice_icon.png">
							</image>
							<view>批量开票</view>
						</view>
					</view>
					<!-- <swiper :current="orderMenuIndex" :duration="300" :show-scrollbar="false"
						class="history-order-swiper"> -->
					<!-- 门店订单 begin -->
					<!-- <swiper-item @touchmove.stop="handleSwiperItemChange"> -->
					<scroll-view scroll-y="true" class="orders-scroll">
						<view class="wrapper">
							<view class="order-list">
								<navigator class="order" v-for="(order, index) in orders" :key="index"
									open-type="navigate" :url="'/pages/order/detail?id=' + order.id">
									<view class="header">
										<view class="flex-fill font-size-medium">{{ order.address }}</view>
										<view class="status">
											<view>{{order.status}}</view>
											<image
												src="https://mp-cc85421a-533b-4b68-b797-f0a3eba335d0.cdn.bspapp.com/common/black_arrow_right.png">
											</image>
										</view>
									</view>
									<scroll-view scroll-x>
										<view class="images">
											<image :src="item" v-for="(item, index) in order.productImages"
												:key="index">
											</image>
										</view>
									</scroll-view>
									<view class="info">
										<view class="left">
											<view>订单编号：{{ order.no }}</view>
											<view>下单时间：{{ order.created_at }}</view>
										</view>
										<view class="right">
											￥{{ order.cartAmount }}
										</view>
									</view>
									<view class="action">
										<button type="default" hover-class="none">开发票</button>
										<button type="default" hover-class="none">查看评论</button>
										<button type="primary" plain hover-class="none">再来一单</button>
									</view>
								</navigator>
							</view>
						</view>
					</scroll-view>
					<!-- </swiper-item> -->
					<!-- 门店订单 end -->
					<!-- 百货订单 begin -->
					<!-- 						<swiper-item @touchmove.stop="handleSwiperItemChange">
							<view class="store-order-wrapper">
								<image src="/static/images/order/default_img_order.png"></image>
								<view>您还没有下单</view>
								<view>快去百货商城逛逛吧</view>
							</view>
						</swiper-item> -->
					<!-- 百货订单 end -->
					<!-- </swiper> -->
				</view>
			</swiper-item>
			<!-- 历史订单 end -->
		</swiper>
		<!-- <image src="https://go.cdn.heytea.com/storage/ad/2020/05/20/1a389117c2fb44d5bcad4a910a68246c.jpg"></image> -->
	</view>
</template>

<script>
	import UQRCode from 'uqrcodejs';
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	import cloudApi from '../../common/cloudApi';
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				tabIndex: 0,
				orderMenuIndex: 0,
				orders: [], //历史订单
				currentOrders: [], //当前订单
				storeOrders: [],
				modules: []
			}
		},
		components: {
			uniPopup
		},
		onShow() {
			this.loadCurrentOrders();
			this.getOrders();
		},
		onLoad() {
			// const qr = new UQRCode();
			// qr.data = 'uQRCode';
			// qr.make();
			// this.modules = qr.modules;
		},
		computed: {
			batchInvoiceVisible() {
				return (!this.orderMenuIndex && this.orders.length) || (this.orderMenuIndex && this.storeOrders.length)
			},
			...mapState(['isLogin', 'userInfo'])
		},
		methods: {
			createQr() {
				const qr = new UQRCode();
				qr.data = 'uQRCode';
				qr.make();
				this.modules = qr.modules;
			},
			qucan(index) {
				this.$refs.popup[index].open();
				this.createQr();
			},
			gotoxiadan() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			switchTab(index) {
				if (this.tabIndex === index) return
				this.tabIndex = index
				if (index === 0) {
					this.loadCurrentOrders();
				} else {
					this.getOrders();
				}
			},
			handleSwiperItemChange() { //禁止手动滑动
				return true
			},
			completeOrder(item) {
				// 调用云函数来更改订单状态
				console.log(item)
				cloudApi.call({
					name: 'updateOrderStatus', // 你的云函数名称，请根据实际情况修改
					data: {
						userId: this.userInfo.userId,
						orderid: item.no,
						newStatus: '已完成' // 更改状态
					},
					success: (res) => {
						if (res.result.code === 0) {
							// 更新订单状态成功
							this.loadCurrentOrders();
							// 在此处处理后续逻辑，例如刷新订单列表
						} else {
							return
						}
					}
				})

			},
			getOrders() {
				if (this.isLogin) {
					// 用户已登录，调用云函数获取订单数据
					cloudApi.call({
						name: "orders",
						data: {
							userId: this.userInfo.userId
						},
						success: (res) => {
							this.orders = res.result.data;
							console.log(this.orders)
						}
					});
				} else {
					uni.showModal({
						title: '登录提示',
						content: '请先登录以查看订单信息。',
						success: (res) => {
							if (res.confirm) {
								// 用户点击确定，跳转到登录页面或执行登录逻辑
								uni.switchTab({
									url: '/pages/my/my'
								});
							}
						}
					});
				}


				// if (!this.orderMenuIndex) {
				// 	cloudApi.call({
				// 		name: "orders",
				// 		data: {},
				// 		success: (res) => {
				// 			this.orders = res.result.data
				// 		}
				// 	})
				// } else {
				// 	uni.showModal({

				// 	})
				// }
			},
			loadCurrentOrders() {
				if (this.isLogin) {
					cloudApi.call({
						name: 'neworders', // 云函数名称，请根据实际情况修改
						data: {
							userId: this.userInfo.userId
						}, // 可以传递参数给云函数
						success: (res) => {
							console.log(res);
							this.currentOrders = res.result.data;
							// this.currentOrders = res.orders;
						}
					});
				} else {
					uni.showModal({
						title: '登录提示',
						content: '请先登录以查看订单信息。',
						success: (res) => {
							if (res.confirm) {
								// 用户点击确定，跳转到登录页面或执行登录逻辑
								uni.switchTab({
									url: '/pages/my/my'
								});
							}
						}
					});
				}
			}
		}
	};
</script>


<style lang="scss" scoped>
	page {
		background-color: #f6f6f6;
	}

	.navbar {
		height: calc(44px + var(--status-bar-height));
		/* #ifdef H5 */
		height: 44px;
		/* #endif */
		display: flex;
		background-color: #FFFFFF;
	}

	.talk-btn {
		height: 32px;
		margin-left: 10px;
		margin-top: 30px;
		/* #ifdef H5 */
		margin-top: 6px;
		/* #endif */
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: $font-size-sm !important;
		padding: 0 20rpx;
		border-radius: 50rem !important;

		image {
			width: 40rpx;
			height: 40rpx;
			margin-right: $spacing-row-sm;
		}
	}

	.tabbar {
		height: 100rpx;
		background-color: $bg-color-white;
		display: flex;
		align-items: center;
		justify-content: space-around;

		.item {
			height: 100%;
			font-size: $font-size-lg;
			color: $text-color-assist;
			font-weight: 400 !important;
			display: flex;
			align-items: center;

			&.active {
				color: $text-color-base;
				border-bottom: 4rpx solid $text-color-base;
			}
		}
	}

	.swiper {
		width: 100%;
		height: calc(100vh - 44px - var(--status-bar-height) - 110rpx);
		/* #ifdef H5 */
		height: calc(100vh - 44px - var(--status-bar-height) - 110rpx - 100rpx);
		/* #endif */
	}

	.no-order-content {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		image {
			width: 300rpx;
			height: 300rpx;
			margin-bottom: 50rpx;
		}

		.tips {
			color: $text-color-assist;
			display: flex;
			flex-direction: column;
			align-items: center;
			line-height: 1.2rem !important;
			margin-bottom: 70rpx;
		}

		button {
			width: 50%;
		}
	}

	.current-order-content {
		width: 100%;
		height: 100%;
		position: relative;
	}

	.current-orders-scroll {
		width: 100%;
		height: 100%;
	}

	.order-list {
		display: flex;
		width: 100%;
		flex-direction: column;

		.order {
			background-color: $bg-color-white;
			padding: 30rpx 40rpx;
			margin-bottom: 18rpx;

			.header {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.status {
					font-size: $font-size-base;
					color: $text-color-grey;
					display: flex;
					align-items: center;

					image {
						width: 30rpx;
						height: 30rpx;
						margin-left: $spacing-row-sm;
					}
				}
			}

			.images {
				display: flex;
				padding: 30rpx 0;

				image {
					flex-shrink: 0;
					width: 150rpx;
					height: 112.5rpx;
				}
			}

			.info {
				display: flex;
				align-items: center;
				margin-bottom: 30rpx;

				.left {
					flex: 1;
					display: flex;
					flex-direction: column;
					font-size: $font-size-base;
					color: $text-color-grey;
				}

				.right {
					font-size: $font-size-lg;
					color: $text-color-base;
				}
			}

			.action {
				display: flex;
				justify-content: flex-end;
				align-items: center;

				button {
					margin-left: 30rpx;
					font-size: $font-size-sm;
				}
			}
		}
	}

	.history-order {
		width: 100%;
		height: 100%;
		position: relative;

		.menu {
			padding: 18rpx 30rpx;
			display: flex;
			align-items: center;
			font-size: $font-size-base;
			color: $text-color-grey;
			position: fixed;
			top: 0;
			left: 0;
			right: 0;
			z-index: 10;

			.item {
				padding: 14rpx 30rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				image {
					width: 40rpx;
					height: 40rpx;
					margin-right: 10rpx;
				}

				&.active {
					color: $color-primary;
					background-color: $bg-color-white;
				}
			}
		}

		.history-order-swiper {
			width: 100%;
			height: 100%;
		}
	}

	// .store-order-wrapper {
	// 	width: 100%;
	// 	height: 100%;
	// 	display: flex;
	// 	flex-direction: column;
	// 	align-items: center;
	// 	justify-content: center;
	// 	font-size: $font-size-base;
	// 	color: $text-color-assist;
	// 	line-height: 1.3rem !important;

	// 	image {
	// 		width: 400rpx;
	// 		height: 333rpx;
	// 		margin-bottom: 40rpx;
	// 	}
	// }

	.orders-scroll {
		width: 100%;
		height: 100%;
		padding-top: 104rpx;
	}

	.order-list {
		display: flex;
		width: 100%;
		flex-direction: column;

		.order {
			background-color: $bg-color-white;
			padding: 30rpx 40rpx;
			margin-bottom: 18rpx;

			.header {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.status {
					font-size: $font-size-base;
					color: $text-color-grey;
					display: flex;
					align-items: center;

					image {
						width: 30rpx;
						height: 30rpx;
						margin-left: $spacing-row-sm;
					}
				}
			}

			.images {
				display: flex;
				padding: 30rpx 0;

				image {
					flex-shrink: 0;
					width: 150rpx;
					height: 112.5rpx;
				}
			}

			.info {
				display: flex;
				align-items: center;
				margin-bottom: 30rpx;

				.left {
					flex: 1;
					display: flex;
					flex-direction: column;
					font-size: $font-size-base;
					color: $text-color-grey;
				}

				.right {
					font-size: $font-size-lg;
					color: $text-color-base;
				}
			}

			.action {
				display: flex;
				justify-content: flex-end;
				align-items: center;

				button {
					margin-left: 30rpx;
					font-size: $font-size-sm;
				}
			}
		}
	}
</style>